-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix: panic when pressing Shift + TAB in a TextEdit #3878
Conversation
panic when pressing Shift + TAB in a TextEdit emilk#3846 rev() is panic with UTF8 Not byte_index It's char_index ( for UTF8 )
CCursor::new(line_start_index) | ||
} | ||
|
||
pub fn create_char_line_indexes(text: &str) -> Vec<usize> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you use CCursor
instead of usize
, the code becomes self-documenting
pub fn create_char_line_indexes(text: &str) -> Vec<usize> { | |
pub fn create_char_line_indexes(text: &str) -> Vec<CCursor> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If CCursor is used instead of usize,
it seems handling functions might become difficult,
so I'm not sure if it's a good approach.
I will respect your final opinion.
By the way, the create_char_line_indexes()
function is also used
in the next Pull Request related to TAB.
let position = text | ||
.chars() | ||
.rev() | ||
.skip(chars_count - current_index.index) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What was wrong with the old code?
Maybe it just needs to handle the char_count < current_index.index
case?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since UTF-8 characters use 1 to 4 bytes,
using rev()
can cause a panic and terminate the program.
You can test it by copying various UTF-8 characters
and using them in multiline().
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mix the UTF8 characters with English, fill the screen ( with them using multiline(), egui_demo_app, web demo )
, and pressing Shift + TAB here and there and here and there.
Please let me know if you can't find the panic occurrence.
Let me explain it to you again.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's sample TEXT.
pressing Shift + TAB here and there and here and there.
ABC äö 日本語 ABC äö 日本語
ABC äö 日本語 ABC äö 日本語
ABC äö 日本語 ABC äö 日本語
ABC äö 日本語 ABC äö 日本語
ABC äö 日本語 ABC äö 日本語
ABC äö 日本語 ABC äö 日本語
ABC äö 日本語 ABC äö 日本語
ABC äö 日本語 ABC äö 日本語
ABC äö 日本語 ABC äö 日本語
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are two more instances of rev()
in text_cursor_state.rs
,
fortunately, those two do not cause panics
but instead incorrectly select a block where UTF8 characters exist.
I will register this as a separate issue later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@emilk
Have you noticed the panic occurring?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't have time for this right now. I believe you that here is a panic in egui, but I don't think the panic is in the call to .rev()
, and so I think a much simpler solution to your problem can be found.
Run in debug mode and RUST_BACKTRACE=1 and paste the stack-trace to the actual panic.
emilk#3878 panic patch
…milk#3984) * Closes emilk#3846 * Closes emilk#3878 Dear emilk. Leaving aside other function, I think this is all you need to fix to patch the panic that occurs when Shift + TAB. Thank you, emilk.
panic when pressing Shift + TAB in a TextEdit #3846
rev() is panic with UTF8
Not byte_index
It's char_index ( for UTF8 )